package com.bilibili.juc.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author zuife
 * @description
 * @since 2024-04-28 21:08:42
 */
public class InterruptDemo4 {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t isInterrupted() 被修改为true,程序停止");
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //如果出现异常，说明中断标志被清除了
                    //所以需要重新设置中断标志
                    Thread.currentThread().interrupt();
                }
                System.out.println("t1------------ hello interrupt api");
            }
        }, "t1");
        t1.start();
        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        new Thread(() -> {
//            t1.interrupt(); //设置一个中断协商标志
//        }, "t2").start();
        t1.interrupt();
    }
}
